package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.concurrent.CompletableFuture;

/**
 * @Author : Cxp
 * @Time : 2024-08-14
 * @Description : 秦始皇陵一键跳转至活动已解锁的最深层
 */

@Cmd(code = GameProtocol.S_TOMB_ONE_CLICK_JUMP, desc = "一键跳转至活动已解锁的最深层")
public class TombOneClickJumpCmd extends AbstractCommand {

    private Tomb.TombOneClickJumpReq reqMsg; // 暂无实际用途，可供后来添加
    private Tomb.TombOneClickJumpResp.Builder respMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = Tomb.TombOneClickJumpReq.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respMsg = Tomb.TombOneClickJumpResp.newBuilder();

        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) { // 判断活动信息是否为空
            this.respMsg.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_ONE_CLICK_JUMP, this.respMsg);
            return;
        }

        int ret = player.getModule(TombModule.class).isOpen(true);
        if (ret > 0) { // 判断活动是否开启
            this.respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOMB_ONE_CLICK_JUMP, this.respMsg);
            return;
        }

        // 跨服请求，异步调用返回
        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(),
                CrossProtocol.C_TOMB_ONE_CLICK_JUMP, activityInfo.getActivityId(), reqMsg.toBuilder(), this.respMsg);
        future.thenAccept(syncResponse -> {
            player.sendPacket(ClientProtocol.U_TOMB_ONE_CLICK_JUMP, this.respMsg);
        });
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
